package com.etc.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Dept;
import com.etc.service.DeptService;
import com.etc.util.AbsResponseData;
import com.etc.util.ResponseData;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @ClassName: DeptController
 * @Description: @RestController = @Controller + @ResponseBody
 * @author knowno
 * @date 2022年4月7日 上午11:04:24
 *
 */
@RestController
@Api(value = "部门管理控制器", description = "部门管理接口")
public class DeptController {

	// 自己定义日志对象,在程序中记录日志
	// org.apache.logging.log4j.Logger; 接口
	// org.apache.logging.log4j.LogManager 日志管理器

	Logger logger = LogManager.getLogger(DeptController.class);

	// 注入一个service
	/**
	 * @Fields service : TODO(用一句话描述这个变量表示什么)
	 */
	@Autowired
	private DeptService service;

	@PostMapping(value = "dept")
	@ApiOperation(value = "部门的增加", httpMethod = "POST", notes = "部门新增操作", response = AbsResponseData.class)
	public AbsResponseData add(@RequestBody Dept dept) {
		int result = service.add(dept);

		logger.info("info :" + result);
		AbsResponseData ard = new AbsResponseData();
		if (result != 1) {
			ard.setMsg("fail");
		}

		return ard;

	}
	
	
	@PutMapping(value = "dept")
	@ApiOperation(value = "部门的修改", httpMethod = "PUT", notes = "部门修改", response = AbsResponseData.class)
	public AbsResponseData update(@RequestBody Dept dept) {
		int result = service.update(dept);
		logger.info("info :" + result);
		AbsResponseData ard = new AbsResponseData();
		if (result != 1) {
			ard.setMsg("fail");
		}

		return ard;

	}

	@DeleteMapping(value = "dept/{id}")
	@ApiOperation(value = "部门的删除", httpMethod = "DELETE", notes = "部门删除", response = AbsResponseData.class)
	public AbsResponseData update(@PathVariable("id") int id) {
		int result = service.delete(id);
		logger.info("info :" + result);
		AbsResponseData ard = new AbsResponseData();
		if (result != 1) {
			ard.setMsg("fail");
		}

		return ard;

	}
	
	/**
	 * 
	 * @param page     页码
	 * @param limit    每页记录数
	 * @param keywords 查询关键字
	 * @return
	 */
	@GetMapping(value = "dept")
	@ApiOperation(value = "部门的模糊查询", httpMethod = "GET", notes = "部门模糊查询方法", response = ResponseData.class)
	public ResponseData<Dept> getDept(@RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
			@RequestParam(value = "limit", required = true, defaultValue = "10") Integer limit,
			@RequestParam(value = "keywords", required = true, defaultValue = "") @ApiParam(value = "关键字", name = "keywords", required = false) String keywords) {
		Page<Dept> pages = service.getByPage(page, limit, keywords);
		// 调用logger的方法

//		logger.debug("debug: " + pages);
		logger.info("info :" + pages);
//		logger.warn("warn: "+pages);
//		logger.error("error: " + pages);

		ResponseData<Dept> rdata = new ResponseData<Dept>(pages.getTotal(), pages.getResult());

		return rdata;
	}

}
